﻿using NaireSystem.Api;
using NaireSystem.DBEntity;
using NaireSystem.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TYBase;
using TYControls;

namespace NaireSystem.Views.UserManager
{
    public partial class CtrlDictManager : TYUserControl
    {
        ViewModelDictManager vm = new ViewModelDictManager();
        public CtrlDictManager()
        {
            InitializeComponent();
            this.DataContext = vm;
            this.Loaded += CtrlDictManager_Loaded;
        }

        private void CtrlDictManager_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private async void BtnClick_Search(object sender, RoutedEventArgs e)
        {
            string dictName = vm.DictName;
            string dictType = vm.DictType;
            vm.ListDictType.Clear();
            var rData = await Run(() =>
              {
                  return DictTypeApi.GetList(new DBEntity.Tbl_DictType { dict_type = dictType, dict_type_name = dictName });
              });
            if (rData.code != 0)
            {
                this.ShowMsg(rData.msg);
                return;
            }
            rData.data.ForEach(x =>
            {
                vm.ListDictType.Add(new DictTypeModel
                {
                    CreateTime = x.create_time.AsString(),
                    DictName = x.dict_type_name,
                    DictType = x.dict_type,
                    Index = vm.ListDictType.Count + 1,
                    id = x.dict_type_id,
                });
            });
            vm.SelectedDictType = vm.ListDictType.FirstOrDefault();
        }

        private async void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            vm.ListDictData.Clear();
            if (vm.SelectedDictType == null)
                return;
            string dict_type = vm.SelectedDictType.DictType;

            var rData = await Run(() =>
            {
                return DictDataApi.GetList(new DBEntity.Tbl_DictData { dict_type = dict_type });
            });
            if (rData.code != 0)
            {
                this.ShowMsg(rData.msg);
                return;
            }
            rData.data.ForEach(x =>
            {
                vm.ListDictData.Add(new DictDataModel
                {
                    CreateTime = x.create_time.AsString(),
                    DictLabel = x.dict_label,
                    DictValue = x.dict_value,
                    Index = vm.ListDictData.Count + 1,
                    dict_type = x.dict_type,
                    dict_data_id = x.dict_data_id,
                });
            });

        }

        private async void BtnClick_AddDictType(object sender, RoutedEventArgs e)
        {
            ViewEditDictType view = new ViewEditDictType(null);
            if (view.ShowDialog() != true)
                return;
            Tbl_DictType dictType = new Tbl_DictType() { dict_type = view.StrDictType, dict_type_name = view.StrDictTypeName };

            var rData = await Run(() =>
             {
                 return DictTypeApi.AddItem(dictType);
             });
            if (rData.code != 0)
            {
                this.ShowMsg(rData.msg);
                return;
            }
            if (rData.data)
            {
                this.ShowMsg(message: "添加成功!");
            }
            BtnClick_Search(null, null);
        }

        private async void BtnClick_AddDictData(object sender, RoutedEventArgs e)
        {
            if (vm.SelectedDictType == null)
                return;
            ViewEditDictData view = new ViewEditDictData(null);
            if (view.ShowDialog() != true)
                return;
            Tbl_DictData dictType = new Tbl_DictData()
            {
                dict_label = view.StrDictLabel,
                dict_type = vm.SelectedDictType.DictType,
            };

            var rData = await Run(() =>
            {
                return DictDataApi.AddItem(dictType);
            });
            if (rData.code != 0)
            {
                this.ShowMsg(rData.msg);
                return;
            }
            if (rData.data)
            {
                this.ShowMsg(message: "添加成功!");
            }
            DataGrid_SelectionChanged(null, null);
        }
    }
}
